package com.hd.trans.utils

import android.animation.Keyframe
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.view.View
import android.view.animation.DecelerateInterpolator

/**
 * @Auther admin
 */
object AnimationUtil {
    /**
     * 抖动动画
     */
    fun shake(
            view: View?,
            scaleSmall: Float,
            scaleLarge: Float,
            shakeDegrees: Float,
            duration: Long
    ) {
        if (view == null) {
            return
        }
        //先变小后变大
        val scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(
                View.SCALE_X,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        )
        val scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(
                View.SCALE_Y,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        )
        //先往左再往右
        val rotateValuesHolder = PropertyValuesHolder.ofKeyframe(
            View.ROTATION,
            Keyframe.ofFloat(0f, 0f),
            Keyframe.ofFloat(0.2f, -shakeDegrees),
            Keyframe.ofFloat(0.4f, shakeDegrees),
            Keyframe.ofFloat(0.6f, -shakeDegrees),
            Keyframe.ofFloat(0.8f, shakeDegrees),
            Keyframe.ofFloat(1.0f, 0f)
        )
        val objectAnimator = ObjectAnimator.ofPropertyValuesHolder(
            view,
            scaleXValuesHolder,
            scaleYValuesHolder,
            rotateValuesHolder
        )
        objectAnimator.duration = duration
        objectAnimator.start()
    }

    /**
     * 心跳动画
     */
    fun heartBeat(view: View, scale: Float = 0.8f, duration: Long = 1000): ObjectAnimator  {
        val scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, scale, 1.0f)
        val scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, scale, 1.0f)
        val anim = ObjectAnimator.ofPropertyValuesHolder(view, scaleX, scaleY)
        anim.duration = duration
        anim.repeatCount = -1
        anim.interpolator = DecelerateInterpolator()
        anim.startDelay = 100
        anim.start()

        return anim
    }


}